﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using ihongma.DBUtility;
using System.Collections.Generic;//Please add references
namespace ihongma.DAL
{
	/// <summary>
	/// 数据访问类:tag
	/// </summary>
	public partial class tag
	{
		public tag()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperOleDb.GetMaxID("id", "dt_tag"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from dt_tag");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
};
			parameters[0].Value = id;

			return DbHelperOleDb.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public DbHelperAge Add(ihongma.Model.tag model)
		{
            DbHelperAge models = new DbHelperAge();
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into dt_tag(");
			strSql.Append("x,y,width,height,message,photoid)");
			strSql.Append(" values (");
			strSql.Append("@x,@y,@width,@height,@message,@photoid)");
			OleDbParameter[] parameters = {
					new OleDbParameter("@x", OleDbType.Integer,4),
					new OleDbParameter("@y", OleDbType.Integer,4),
					new OleDbParameter("@width", OleDbType.Integer,4),
					new OleDbParameter("@height", OleDbType.Integer,4),
					new OleDbParameter("@message", OleDbType.VarChar,255),
					new OleDbParameter("@photoid", OleDbType.Integer,4)};
			parameters[0].Value = model.x;
			parameters[1].Value = model.y;
			parameters[2].Value = model.width;
			parameters[3].Value = model.height;
			parameters[4].Value = model.message;
			parameters[5].Value = model.photoid;
            models.StrSql = strSql.ToString();
            models.OleDbParameterList = parameters;
            //int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
            //if (rows > 0)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
            return models;
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public DbHelperAge Update(ihongma.Model.tag model)
		{
            DbHelperAge models = new DbHelperAge();
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update dt_tag set ");
			strSql.Append("x=@x,");
			strSql.Append("y=@y,");
			strSql.Append("width=@width,");
			strSql.Append("height=@height,");
			strSql.Append("message=@message,");
			strSql.Append("photoid=@photoid");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@x", OleDbType.Integer,4),
					new OleDbParameter("@y", OleDbType.Integer,4),
					new OleDbParameter("@width", OleDbType.Integer,4),
					new OleDbParameter("@height", OleDbType.Integer,4),
					new OleDbParameter("@message", OleDbType.VarChar,255),
					new OleDbParameter("@photoid", OleDbType.Integer,4),
					new OleDbParameter("@id", OleDbType.Integer,4)};
			parameters[0].Value = model.x;
			parameters[1].Value = model.y;
			parameters[2].Value = model.width;
			parameters[3].Value = model.height;
			parameters[4].Value = model.message;
			parameters[5].Value = model.photoid;
			parameters[6].Value = model.id;
            models.OleDbParameterList = parameters;
            models.StrSql = strSql.ToString();
            return models;
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
        public DbHelperAge Delete(int id)
		{
            DbHelperAge age = new DbHelperAge();
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_tag ");
            strSql.Append(" where photoid=@photoid");
			OleDbParameter[] parameters = {
					new OleDbParameter("@photoid", OleDbType.Integer,4)
};
			parameters[0].Value = id;

            age.OleDbParameterList = parameters;
            age.StrSql = strSql.ToString();
            return age;
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
        public DbHelperAge DeleteList(string idlist)
		{
            DbHelperAge age = new DbHelperAge();
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_tag ");
            strSql.Append(" where photoid in("+idlist+")  ");
            OleDbParameter[] parameters = {
                   new OleDbParameter("@photoid", OleDbType.Integer,4)};
            age.OleDbParameterList = parameters;
            age.StrSql = strSql.ToString();
            return age;
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ihongma.Model.tag GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,x,y,width,height,message,photoid from dt_tag ");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
};
			parameters[0].Value = id;

			ihongma.Model.tag model=new ihongma.Model.tag();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["x"]!=null && ds.Tables[0].Rows[0]["x"].ToString()!="")
				{
					model.x=int.Parse(ds.Tables[0].Rows[0]["x"].ToString());
				}
				if(ds.Tables[0].Rows[0]["y"]!=null && ds.Tables[0].Rows[0]["y"].ToString()!="")
				{
					model.y=int.Parse(ds.Tables[0].Rows[0]["y"].ToString());
				}
				if(ds.Tables[0].Rows[0]["width"]!=null && ds.Tables[0].Rows[0]["width"].ToString()!="")
				{
					model.width=int.Parse(ds.Tables[0].Rows[0]["width"].ToString());
				}
				if(ds.Tables[0].Rows[0]["height"]!=null && ds.Tables[0].Rows[0]["height"].ToString()!="")
				{
					model.height=int.Parse(ds.Tables[0].Rows[0]["height"].ToString());
				}
				if(ds.Tables[0].Rows[0]["message"]!=null && ds.Tables[0].Rows[0]["message"].ToString()!="")
				{
					model.message=ds.Tables[0].Rows[0]["message"].ToString();
				}
				if(ds.Tables[0].Rows[0]["photoid"]!=null && ds.Tables[0].Rows[0]["photoid"].ToString()!="")
				{
					model.photoid=int.Parse(ds.Tables[0].Rows[0]["photoid"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Model.tag> GetList(string strWhere)
		{
            List<Model.tag> listtag = new List<ihongma.Model.tag>();
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,x,y,width,height,message,photoid ");
			strSql.Append(" FROM dt_tag ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            DataSet ds = DbHelperOleDb.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
               
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    Model.tag model = new ihongma.Model.tag();
                    if (row["id"] != null && row["id"].ToString() != "")
                    {
                        model.id = int.Parse(row["id"].ToString());
                    }
                    if (row["x"] != null && row["x"].ToString() != "")
                    {
                        model.x = int.Parse(row["x"].ToString());
                    }
                    if (row["y"] != null && row["y"].ToString() != "")
                    {
                        model.y = int.Parse(row["y"].ToString());
                    }
                    if (row["width"] != null && row["width"].ToString() != "")
                    {
                        model.width = int.Parse(row["width"].ToString());
                    }
                    if (row["height"] != null && row["height"].ToString() != "")
                    {
                        model.height = int.Parse(row["height"].ToString());
                    }
                    if (row["message"] != null && row["message"].ToString() != "")
                    {
                        model.message = row["message"].ToString();
                    }
                    if (row["photoid"] != null && row["photoid"].ToString() != "")
                    {
                        model.photoid = int.Parse(row["photoid"].ToString());
                    }
                    listtag.Add(model);
                }
               
            }
            return listtag;
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OleDbParameter[] parameters = {
					new OleDbParameter("@tblName", OleDbType.VarChar, 255),
					new OleDbParameter("@fldName", OleDbType.VarChar, 255),
					new OleDbParameter("@PageSize", OleDbType.Integer),
					new OleDbParameter("@PageIndex", OleDbType.Integer),
					new OleDbParameter("@IsReCount", OleDbType.Boolean),
					new OleDbParameter("@OrderType", OleDbType.Boolean),
					new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
					};
			parameters[0].Value = "dt_tag";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

